<?php

class ViewCourse extends MY_Controller {

    public function __construct() {
        parent::__construct();
        
        if($this->session->userdata('user_type') != 1 || $this->session->userdata('user_type') == null){
            show_error("You have no permission here");
        }
    }

    function index() {
        $this->load->library('pagination');
        $this->load->library('table');

        
        $config['base_url'] = 'http://localhost/AsNew/ci/index.php/ViewCourse/index'; 
        $config['total_rows'] = $this->db->get('COURSE')->num_rows(); 
        $config['per_page'] = 10;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';
        
        $this->pagination->initialize($config);

        $this->load->model('Course_Model');
        $query = $this->Course_Model->getAllCourse($config['per_page'], $this->uri->segment(3));
        $result = $query->result();
        
        $this->table->set_heading('No', 'Course Code', 'Course Name', 'Hours', 'Prices', 'Detail', 'Manage'); //Create columns
        
        if($query->num_rows() > 0)
        {   
            for($i=0;$i<count($result);$i++):
                $row = $result[$i];
                $this->table->add_row(
                        $i+1, 
                        $row->CODE, 
                        $row->NAME, 
                        $row->HOURS, 
                        $row->PRICE, 
                        $row->DETAIL, 
                        anchor( base_url().'index.php/ViewCourse/getCourse/'.$row->ID, '<img src="'.base_url().'images/view.png" alt="View" />' ) 
                        ); 
            endfor;

        }else{
            $this->table->add_row('No results found','','','');
        }

        $tmpl = array ( 
                        'table_open' => '<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">',
                        'heading_cell_start'  => '<th class="table-header-repeat line-left"><a href>',
                        'heading_cell_end'    => '</a></th>',
                        'table_close'         => '</table>');

        $this->table->set_template($tmpl);

        $this->data['main_content'] = 'course';
        $this->load->view('includes/template', $this->data);
    }
    
    function getCourse($courseID) {
        $this->load->Model('Course_Model');
        $query = $this->Course_Model->getCourseByID($courseID);
        
        if($query->num_rows() == 1) // if the user's credentials validated...
        {
            $row = $query->row();

            $course = array(
                                    'id' => $row->ID,
                                    'course_name' => $row->NAME,
                                    'course_code' => $row->CODE,
                                    'prices' => $row->PRICE,
                                    'hours' => $row->HOURS,
                                    'detail' => $row->DETAIL,
                                    'videos' => $row->VIDEOS
            );
            $this->data['course'] = $course;
        }
        
        $courseCode = array(
                'disabled'    => 'diabled',
                'id'    => 'course_code',
                'name'  => 'course_code'
            );
        
        $courseName= array(
                'disabled'    => 'diabled',
                'id'    => 'course_name',
                'name'  => 'course_name'
            );
        
        $courseHours = array(
                'disabled'    => 'diabled',
                'id'    => 'course_hours',
                'name'  => 'course_hours'
            );
        
        $courseNumVideos = array(
                'disabled'    => 'diabled',
                'id'    => 'course_num_videos',
                'name'  => 'course_num_videos'
            );
        
        $coursePrice = array(
                'disabled'    => 'diabled',
                'id'    => 'course_price',
                'name'  => 'course_price'
            );
        
        $courseDetail = array(
                'disabled'    => 'diabled',
                'id'    => 'course_detail',
                'name'  => 'course_detail'
            );
        
        $btnReset = array(
                'disabled'    => 'diabled',
                'id'    => 'btnReset',
                'name'  => 'btnReset'
            );
        
        $back = array(
                'id'    => 'back',
                'name'  => 'back'
            );
        
        $this->data['courseCode'] = $courseCode;
        $this->data['courseName'] = $courseName;
        $this->data['courseHours'] = $courseHours;
        $this->data['courseNumVideos'] = $courseNumVideos;
        $this->data['coursePrice'] = $coursePrice;
        $this->data['courseDetail'] = $courseDetail;
        $this->data['btnReset'] = $btnReset;
        $this->data['back'] = $back;
        
        $this->data['main_title'] = "View Course";
        $this->data['action'] = 'view';
        $this->data['main_content'] = 'add_course';
        $this->load->view('includes/template', $this->data);
    }


}
?>
